package com.isdepci.project.carbon.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.isdepci.project.carbon.domain.*;
import com.isdepci.project.carbon.domain.param.StatisticalResult;
import com.isdepci.project.carbon.mapper.*;
import com.isdepci.project.carbon.service.IResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 多维分析结果Service业务层处理
 *
 * @author isdepci
 * @date 2025-05-14
 */
@Service
public class ResultServiceImpl implements IResultService {
    @Autowired
    private BBlastEmissionMapper bBlastEmissionMapper;
    @Autowired
    private BCarbonTradeMapper bCarbonTradeMapper;
    @Autowired
    private BCcerTradeMapper bCcerTradeMapper;
    @Autowired
    private BDirectReleaseMapper bDirectReleaseMapper;
    @Autowired
    private BEleEmissionMapper bEleEmissionMapper;
    @Autowired
    private BGasFuelMapper bGasFuelMapper;
    @Autowired
    private BGreenCarbonSinkMapper bGreenCarbonSinkMapper;
    @Autowired
    private BGreenCertificateMapper bGreenCertificateMapper;
    @Autowired
    private BHeatEmissionMapper bHeatEmissionMapper;
    @Autowired
    private BLiquidFuelMapper bLiquidFuelMapper;
    @Autowired
    private BNitreEmissionMapper bNitreEmissionMapper;
    @Autowired
    private BRecyclingCarbonMapper bRecyclingCarbonMapper;
    @Autowired
    private BSolidFuelMapper bSolidFuelMapper;
    @Autowired
    private BSulfurEmissionMapper bSulfurEmissionMapper;
    @Autowired
    private BWeldHeatEmissionMapper bWeldHeatEmissionMapper;


    @Override
    public JSONObject selectResult(StatisticalResult result) {
        JSONObject response = new JSONObject();
        // 获取日期类型、开始日期和结束日期
        Integer dateType = result.getDateType();
        // 获取数据类型
//        List<Integer> dataTypes = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> dataTypes = result.getDataType();
        // 创建总排放量
        BigDecimal totalEmission = BigDecimal.ZERO;

        List<Object> finalList = new ArrayList<>();
        List<BBlastEmission> blastEmissions = new ArrayList<>();
        List<BCarbonTrade> carbonTrades = new ArrayList<>();
        List<BCcerTrade> ccerTrades = new ArrayList<>();
        List<BDirectRelease> directReleases = new ArrayList<>();
        List<BEleEmission> eleEmissions = new ArrayList<>();
        List<BGasFuel> gasFuels = new ArrayList<>();
        List<BGreenCarbonSink> greenCarbonSinks = new ArrayList<>();
        List<BGreenCertificate> greenCertificates = new ArrayList<>();
        List<BHeatEmission> heatEmissions = new ArrayList<>();
        List<BLiquidFuel> liquidFuels = new ArrayList<>();
        List<BNitreEmission> nitreEmissions = new ArrayList<>();
        List<BRecyclingCarbon> recyclingCarbons = new ArrayList<>();
        List<BSolidFuel> solidFuels = new ArrayList<>();
        List<BSulfurEmission> sulfurEmissions = new ArrayList<>();
        List<BWeldHeatEmission> weldHeatEmissions = new ArrayList<>();
        if (dateType == 1) {
            blastEmissions = bBlastEmissionMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-爆破
            carbonTrades = bCarbonTradeMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳交易-碳汇普
            ccerTrades = bCcerTradeMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳交易-ccer
            directReleases = bDirectReleaseMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-直接释放
            eleEmissions = bEleEmissionMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//电力热力-电力
            gasFuels = bGasFuelMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//化石燃料-气体
            greenCarbonSinks = bGreenCarbonSinkMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳抵消-绿化碳汇
            greenCertificates = bGreenCertificateMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳抵消-绿证
            heatEmissions = bHeatEmissionMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//电力热力-热力
            liquidFuels = bLiquidFuelMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//化石燃料-液体
            nitreEmissions = bNitreEmissionMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-脱硝
            recyclingCarbons = bRecyclingCarbonMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳抵消-回收利用
            solidFuels = bSolidFuelMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//化石燃料-固体
            sulfurEmissions = bSulfurEmissionMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-脱硫
            weldHeatEmissions = bWeldHeatEmissionMapper.selectByYear(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-切焊加热
        }
        // 如果日期类型为2（年月），则按年月查询
        else if (dateType == 2) {
            blastEmissions = bBlastEmissionMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-爆破
            carbonTrades = bCarbonTradeMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳交易-碳汇普
            ccerTrades = bCcerTradeMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳交易-ccer
            directReleases = bDirectReleaseMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-直接释放
            eleEmissions = bEleEmissionMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//电力热力-电力
            gasFuels = bGasFuelMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//化石燃料-气体
            greenCarbonSinks = bGreenCarbonSinkMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳抵消-绿化碳汇
            greenCertificates = bGreenCertificateMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳抵消-绿证
            heatEmissions = bHeatEmissionMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//电力热力-热力
            liquidFuels = bLiquidFuelMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//化石燃料-液体
            nitreEmissions = bNitreEmissionMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-脱硝
            recyclingCarbons = bRecyclingCarbonMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//碳抵消-回收利用
            solidFuels = bSolidFuelMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//化石燃料-固体
            sulfurEmissions = bSulfurEmissionMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-脱硫
            weldHeatEmissions = bWeldHeatEmissionMapper.selectByMonth(result.getProjectId(), result.getStartDate(), result.getLastDate());//过程排放-切焊加热
        }
        // 按照小类型筛选数据
        if (dataTypes.contains(1)) { // 固体燃料
            BigDecimal solidFuelEmission = solidFuels.stream().map(BSolidFuel::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("solidFuelEmission", solidFuelEmission);
            finalList.addAll(solidFuels);
            // 累加到总排放量
            totalEmission = totalEmission.add(solidFuelEmission);
        }
        if (dataTypes.contains(2)) { // 气体燃料
            BigDecimal gasFuelEmission = gasFuels.stream().map(BGasFuel::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("gasFuelEmission", gasFuelEmission);
            finalList.addAll(gasFuels);
            // 累加到总排放量
            totalEmission = totalEmission.add(gasFuelEmission);
        }
        if (dataTypes.contains(3)) { // 液体燃料
            BigDecimal liquidFuelEmission = liquidFuels.stream().map(BLiquidFuel::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("liquidFuelEmission", liquidFuelEmission);
            finalList.addAll(liquidFuels);
            // 累加到总排放量
            totalEmission = totalEmission.add(liquidFuelEmission);
        }
        if (dataTypes.contains(4)) { // 电力排放
            BigDecimal powerEmission = eleEmissions.stream().map(BEleEmission::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("powerEmission", powerEmission);
            finalList.addAll(eleEmissions);
            // 累加到总排放量
            totalEmission = totalEmission.add(powerEmission);
        }
        if (dataTypes.contains(5)) { // 热力排放
            BigDecimal heatEmission = heatEmissions.stream().map(BHeatEmission::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("heatEmission", heatEmission);
            finalList.addAll(heatEmissions);
            // 累加到总排放量
            totalEmission = totalEmission.add(heatEmission);
        }
        if (dataTypes.contains(6)) { // 脱硫
            BigDecimal sulfurEmission = sulfurEmissions.stream().map(BSulfurEmission::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("sulfurEmission", sulfurEmission);
            finalList.addAll(sulfurEmissions);
            // 累加到总排放量
            totalEmission = totalEmission.add(sulfurEmission);
        }
        if (dataTypes.contains(7)) { // 脱硝
            BigDecimal nitreEmission = nitreEmissions.stream().map(BNitreEmission::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("nitreEmission", nitreEmission);
            finalList.addAll(nitreEmissions);
            // 累加到总排放量
            totalEmission = totalEmission.add(nitreEmission);
        }
        if (dataTypes.contains(8)) { // 爆破
            BigDecimal blastEmission = blastEmissions.stream().map(BBlastEmission::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("blastEmission", blastEmission);
            finalList.addAll(blastEmissions);
            // 累加到总排放量
            totalEmission = totalEmission.add(blastEmission);
        }
        if (dataTypes.contains(9)) { // 切焊加热
            BigDecimal weldHeatEmission = weldHeatEmissions.stream().map(BWeldHeatEmission::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("weldHeatEmission", weldHeatEmission);
            finalList.addAll(weldHeatEmissions);
            // 累加到总排放量
            totalEmission = totalEmission.add(weldHeatEmission);
        }
        if (dataTypes.contains(10)) { // 直接释放
            BigDecimal directReleaseEmission = directReleases.stream().map(BDirectRelease::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("directReleaseEmission", directReleaseEmission);
            finalList.addAll(directReleases);
            // 累加到总排放量
            totalEmission = totalEmission.add(directReleaseEmission);
        }
        if (dataTypes.contains(11)) { // CCER
            BigDecimal ccerEmission = ccerTrades.stream().map(BCcerTrade::getCcerVolume)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("ccerEmission", ccerEmission);
            for (BCcerTrade ccerTrade : ccerTrades) {
                ccerTrade.setCarbonEmissions(ccerTrade.getCcerVolume());
            }
            finalList.addAll(ccerTrades);
            // 累加到总排放量
            totalEmission = totalEmission.add(ccerEmission);
        }
        if (dataTypes.contains(12)) { // 碳汇普
            BigDecimal carbonEmission = carbonTrades.stream().map(BCarbonTrade::getCarbonVolume)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("carbonEmission", carbonEmission);
            for (BCarbonTrade ccerEmission : carbonTrades) {
                ccerEmission.setCarbonEmissions(ccerEmission.getCarbonVolume());
            }
            finalList.addAll(carbonTrades);
            // 累加到总排放量
            totalEmission = totalEmission.add(carbonEmission);
        }
        if (dataTypes.contains(13)) { //  绿化
            BigDecimal greenCarbonSinkEmission = greenCarbonSinks.stream().map(BGreenCarbonSink::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("greenCarbonSinkEmission", greenCarbonSinkEmission);
            finalList.addAll(greenCarbonSinks);
            // 累加到总排放量
            totalEmission = totalEmission.add(greenCarbonSinkEmission);
        }
        if (dataTypes.contains(14)) { // 回收利用
            BigDecimal recyclingEmission = recyclingCarbons.stream().map(BRecyclingCarbon::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("recyclingEmission", recyclingEmission);
            finalList.addAll(recyclingCarbons);
            // 累加到总排放量
            totalEmission = totalEmission.add(recyclingEmission);
        }
        if (dataTypes.contains(15)) { // 绿证
            BigDecimal greenCertificateEmission = greenCertificates.stream().map(BGreenCertificate::getCarbonEmissions)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            response.put("greenCertificateEmission", greenCertificateEmission);
            finalList.addAll(greenCertificates);
            // 累加到总排放量
            totalEmission = totalEmission.add(greenCertificateEmission);
        }

        response.put("totalEmission", totalEmission);
        response.put("finalList", finalList);

        return response;
    }

}
